package com.example.demo.controller;

import com.example.demo.model.Student;

import com.example.demo.model.Teacher;

import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;

import java.util.List;

@Controller

@RequestMapping("student")

public class StudentController {
    @RequestMapping("search")
    public String search(Student s, ModelMap modelMap) {
        List<Student> studentList = studentRepository.findByName(s.getName());
        modelMap.put("studentList", studentList);
        return "student/list";
    }


    @Autowired
    private StudentRepository studentRepository;

    @RequestMapping("list")

    public String list(ModelMap modelMap) {

        modelMap.put("studentList", studentRepository.findAll());

        return "student/list";

    }

    @RequestMapping("delete/{id}")

    public String delete(@PathVariable Integer id) {

        studentRepository.deleteById(id);

        return "redirect:/student/list";

    }

    @RequestMapping("save")

    public String save(Student s) {

        studentRepository.save(s);

        return "redirect:/student/list";

    }

    @RequestMapping("edit/{id}")

    public String edit(@PathVariable Integer id, ModelMap map) {

        Student s = studentRepository.getReferenceById(id);

        map.put("student", s);

        return "student/edit";

    }

    @RequestMapping("update")

    public String update(Student s) {

        studentRepository.save(s);

        return "redirect:/student/list";

    }

}

